/**
 * 厂家插件文件名，命名规范: [厂家标识]-sdk.js
 */
const plugins = {}

const files = require.context('@/plugins', true, /.*-sdk.js$/)

files.keys().forEach(file => {
    const factoryName = file.match(/\/(\w+)-sdk.js/)[1]
    plugins[factoryName] = files(file).default
})

// 注册顺序，按序判断客户端是否安装
const registry = [
    'dianju',
    'suwell',
]
/**
 * 插件工厂方法，返回插件实例
 * @return {import('@/plugins/suwell-sdk').default | null}
 */
const OfdReaderFactory = (options) => {
    let PluginClass = null

    registry.some(name => {
        if (plugins[name].installed()) {
            console.log('[installed]: ', name)
            PluginClass = plugins[name]
            return true
        }
        return false
    })
    if (PluginClass) {
        return new PluginClass(options)
    }
    return null
}

export default OfdReaderFactory
